<?php

$_event;
$_events = array();

class Event {
  function Event($event,$date) {
    $this->event = $event;
    $this->date = $date;
  }

  function getAddress() {
    return $this->event['ADDRESS'];
  }

  function getType() {
    return $this->event['TYPE'];
  }

  function getDay() {
    return $this->date['DAY'];
  }

  function getTime() {
    return $this->date['TIME'];
  }

  function getTitle() {
    return removeHTML($this->event['TITLE'],'');
  }

  function getLatitude() {
    return $this->event['LATITUDE'];
  }

  function getLongitude() {
    return $this->event['LONGITUDE'];
  }

  function mapLink($title=NULL) {
    $url  = phpSelf() . '?map';
    return '<a href="' . $url . '">' . ($title ? $title : $this->getTitle()) . '</a>';
  }

  function asHTML($title=NULL,$extraAddress=NULL) {
    $res  = '';
    $res .= '<b>' . $this->mapLink($title) . '</b>';
    $res .= '<br/>';
    $res .= $this->getAddress();
    if ($extraAddress) $res .= $extraAddress;
    $res .= '<br/>';
    $res .= '<em>' . $this->getType() . '</em>';
    return $res;
  }
}

function startElement($parser, $name, $attrs)  {
  global $_event;
  global $_events;
  if ($name == 'EVENT' || $name == 'MEETING') {
    $_event = $attrs;
  }
  if ($name == 'DATE') {
    $s = new Event($_event,$attrs);
    $_events []= $s;
  }
}

function endElement($parser, $name) {
  
}

function characterData($parser, $data) {
  
}

function readEvents($file) {
  global $_events;
  $_events = array();
  $xml_parser = xml_parser_create();
  // use case-folding so we are sure to find the tag in $map_array
  xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
  xml_set_element_handler($xml_parser, "startElement", "endElement");
  xml_set_character_data_handler($xml_parser, "characterData");
  if (!($fp = fopen($file, "r"))) {
    die("could not open XML input");
  }
  
  while ($data = fread($fp, 4096)) {
    if (!xml_parse($xml_parser, $data, feof($fp))) {
      die(sprintf("XML error: %s at line %d",
                  xml_error_string(xml_get_error_code($xml_parser)),
                  xml_get_current_line_number($xml_parser)));
    }
  }
  xml_parser_free($xml_parser);
  return $_events;
}

?>